package com.zb.redis;

import cn.dev33.satoken.util.SaFoxUtil;
import com.zb.constant.AccountConstant;
import com.zb.entity.AdmInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

/**
 * @Author WEN
 * @Date 2023/7/10 0010
 **/
@Component
public class RedisLoginTokenUtil {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    private static RedisLoginTokenUtil redisLoginTokenUtil;

    @PostConstruct
    public void init() {
        redisLoginTokenUtil = this;
        redisLoginTokenUtil.redisTemplate = this.redisTemplate;
    }

    /**
     * 生成refrefsh_token
     * @return
     */
    public static String generatorRefreshToken() {
        return SaFoxUtil.getRandomString(128);
    }


    /**
     * 根据[access-token]缓存登录账号信息
     * @param accessToken
     * @param admInfo
     */
    public static void setAdmInfoByAccessToken(String accessToken, AdmInfo admInfo) {
        redisLoginTokenUtil.redisTemplate.opsForValue().set(
                AccountConstant.ACCOUNT + accessToken,
                admInfo,
                admInfo.isRememberMe()? AccountConstant.LOGIN_TOKEN_REMEMBERME_EXPIRE_TIME : AccountConstant.LOGIN_TOKEN_EXPIRE_TIME,
                TimeUnit.SECONDS
        );
    }

    /**
     * 根据[access-token]获取登录账号信息
     * @param accessToken
     * @return
     */
    public static AdmInfo getAdmInfoByAccessToken(String accessToken) {
        return (AdmInfo) redisLoginTokenUtil.redisTemplate.opsForValue().get(AccountConstant.ACCOUNT + accessToken);
    }

    /**
     * 根据[access-token]删除缓存的登录账号信息
     * @param accessToken
     */
    public static void removeAdmInfoByAccessToken(String accessToken) {
        if (Boolean.TRUE.equals(redisLoginTokenUtil.redisTemplate.hasKey(AccountConstant.ACCOUNT + accessToken))) {
            redisLoginTokenUtil.redisTemplate.delete(AccountConstant.ACCOUNT + accessToken);
        }
    }

    /**
     * 根据[refreshToken]缓存[accessToken]
     * @param refreshToken
     * @param accessToken
     * @param rememberMe
     */
    public static void setAccessTokenByRefreshToken(String refreshToken, String accessToken, boolean rememberMe) {
        redisLoginTokenUtil.redisTemplate.opsForValue().set(
                AccountConstant.REFRESH_TOKEN_TO_ACCESS_TOKEN + refreshToken,
                accessToken,
                rememberMe? AccountConstant.LOGIN_TOKEN_REMEMBERME_EXPIRE_TIME : AccountConstant.LOGIN_TOKEN_EXPIRE_TIME,
                TimeUnit.SECONDS
        );
    }

    /**
     * 根据[refreshToken]获取[accessToken]
     * @param refreshToken
     * @return
     */
    public static String getAccessTokenByRefreshToken(String refreshToken) {
        return (String) redisLoginTokenUtil.redisTemplate.opsForValue().get(AccountConstant.REFRESH_TOKEN_TO_ACCESS_TOKEN + refreshToken);
    }

    /**
     * 根据[refreshToken]判断是否登录缓存有[accessToken]
     * @param refreshToken
     * @return
     */
    public static boolean hasAccessTokenByRefreshToken(String refreshToken) {
        return Boolean.TRUE.equals(redisLoginTokenUtil.redisTemplate.hasKey(AccountConstant.REFRESH_TOKEN_TO_ACCESS_TOKEN + refreshToken));
    }

    /**
     * 根据[refreshToken]删除[accessToken]
     * @param refreshToken
     */
    public static void removeAccessTokenByRefreshToken(String refreshToken) {
        redisLoginTokenUtil.redisTemplate.delete(AccountConstant.REFRESH_TOKEN_TO_ACCESS_TOKEN + refreshToken);
    }

    /**
     * 根据[accessToken]缓存[refreshToken]
     * @param accessToken
     * @param refreshToken
     * @param rememberMe
     */
    public static void setRefreshTokenByAccessToken(String accessToken, String refreshToken, boolean rememberMe) {
        redisLoginTokenUtil.redisTemplate.opsForValue().set(
                AccountConstant.REFRESH_TOKEN + accessToken,
                refreshToken,
                rememberMe? AccountConstant.LOGIN_TOKEN_REMEMBERME_EXPIRE_TIME : AccountConstant.LOGIN_TOKEN_EXPIRE_TIME,
                TimeUnit.SECONDS
        );
    }

    /**
     * 根据[accessToken]获取[refreshToken]
     * @param accessToken
     * @return
     */
    public static String getRefreshTokenByAccessToken(String accessToken) {
        return (String) redisLoginTokenUtil.redisTemplate.opsForValue().get(AccountConstant.REFRESH_TOKEN + accessToken);
    }

    /**
     * 根据[accessToken]删除[refreshToken]
     * @param accessToken
     */
    public static void removeRefreshTokenByAccessToken(String accessToken) {
        redisLoginTokenUtil.redisTemplate.delete(AccountConstant.REFRESH_TOKEN + accessToken);
    }

    /**
     * 根据[accessToken]删除登录的access_token（注：此token为sa-token插件自动缓存到redis中的）
     * @param accessToken
     */
    public static void removeLoginAccessToken(String accessToken) {
        redisLoginTokenUtil.redisTemplate.delete(AccountConstant.TOKEN_PREFIX + "token:"+ accessToken);
    }

    /**
     * 根据[accessToken]删除所有登录的token信息
     * @param accessToken
     */
    public static void removeAllTokenByAccessToken(String accessToken) {
        removeAccessTokenByRefreshToken(getRefreshTokenByAccessToken(accessToken));
        removeAdmInfoByAccessToken(accessToken);
        removeRefreshTokenByAccessToken(accessToken);
    }


}
